{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-28T08:15:42.192821Z",
     "start_time": "2025-10-28T08:15:41.798117Z"
    }
   },
   "source": [
    "from langchain_core.messages import AnyMessage, AIMessage\n",
    "from langgraph.graph import StateGraph\n",
    "from langgraph.graph.message import add_messages\n",
    "from typing import Annotated, TypedDict\n",
    "from operator import add\n",
    "\n",
    "\n",
    "class State(TypedDict):\n",
    "    \"\"\"\n",
    "    定义图的状态结构\n",
    "\n",
    "    属性:\n",
    "        messages: 消息列表，使用add_messages操作符进行合并\n",
    "        list_field: 整数列表，使用add操作符进行合并\n",
    "        extra_field: 整数字段，在节点间传递最终值\n",
    "    \"\"\"\n",
    "    messages: Annotated[list[AnyMessage], add_messages]\n",
    "    list_field: Annotated[list[int], add]\n",
    "    extra_field: int\n",
    "\n",
    "\n",
    "def node1(state: State):\n",
    "    \"\"\"\n",
    "    图的第一个节点函数，生成第一条AI消息\n",
    "\n",
    "    参数:\n",
    "        state: 当前状态对象，包含messages、list_field和extra_field\n",
    "\n",
    "    返回:\n",
    "        dict: 包含新消息、列表字段和额外字段的更新字典\n",
    "              messages: 包含\"Hello!\"的AIMessage\n",
    "              list_field: 包含整数10的列表\n",
    "              extra_field: 值为10的整数\n",
    "    \"\"\"\n",
    "    new_message = AIMessage(\"Hello!\")\n",
    "    return {\"messages\": [new_message], \"list_field\": [10], \"extra_field\": 10}\n",
    "\n",
    "\n",
    "def node2(state: State):\n",
    "    \"\"\"\n",
    "    图的第二个节点函数，生成第二条AI消息\n",
    "\n",
    "    参数:\n",
    "        state: 当前状态对象，包含来自前一个节点的消息、列表字段和额外字段\n",
    "\n",
    "    返回:\n",
    "        dict: 包含新消息、列表字段和额外字段的更新字典\n",
    "              messages: 包含\"LangGraph!\"的AIMessage\n",
    "              list_field: 包含整数20的列表\n",
    "              extra_field: 值为20的整数\n",
    "    \"\"\"\n",
    "    new_message = AIMessage(\"LangGraph!\")\n",
    "    return {\"messages\": [new_message], \"list_field\": [20], \"extra_field\": 20}\n",
    "\n",
    "\n",
    "# 构建状态图：定义节点、设置入口点、添加边并编译图\n",
    "graph = (\n",
    "    StateGraph(State)\n",
    "    .add_node(\"node1\", node1)\n",
    "    .add_node(\"node2\", node2)\n",
    "    .set_entry_point(\"node1\")  # 设置入口点为node1\n",
    "    .add_edge(\"node1\", \"node2\")\n",
    "    .compile()\n",
    ")\n",
    "\n",
    "# 准备输入消息并执行图的调用\n",
    "input_message = {\"role\": \"user\", \"content\": \"你好,langgraph\"}\n",
    "result = graph.invoke({\"messages\": [input_message], \"list_field\": [1, 2, 3]})\n",
    "print(result)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'messages': [HumanMessage(content='你好,langgraph', additional_kwargs={}, response_metadata={}, id='b02b98c6-e18b-42ab-abc7-076530b2a2dc'), AIMessage(content='Hello!', additional_kwargs={}, response_metadata={}, id='cdbd77b4-850e-43a8-b447-037145393f66'), AIMessage(content='LangGraph!', additional_kwargs={}, response_metadata={}, id='199d49fc-4e8d-4cfe-881f-653e5c3fa08d')], 'list_field': [1, 2, 3, 10, 20], 'extra_field': 20}\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-28T08:16:21.385877Z",
     "start_time": "2025-10-28T08:16:21.378327Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 遍历消息列表并打印每条消息的详细信息，同时打印结果中的额外字段\n",
    "for message in result[\"messages\"]:\n",
    "    # 格式化打印当前消息的所有属性\n",
    "    message.pretty_print()\n",
    "    # 打印结果字典中的额外信息字段\n",
    "    print(result[\"extra_field\"])"
   ],
   "id": "13844bc54597614f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "================================\u001B[1m Human Message \u001B[0m=================================\n",
      "\n",
      "你好,langgraph\n",
      "20\n",
      "==================================\u001B[1m Ai Message \u001B[0m==================================\n",
      "\n",
      "Hello!\n",
      "20\n",
      "==================================\u001B[1m Ai Message \u001B[0m==================================\n",
      "\n",
      "LangGraph!\n",
      "20\n"
     ]
    }
   ],
   "execution_count": 2
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
